package com.wty.learnsping.aop;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Order(1) //多个切面情况下，拦截的顺序
@Component //要加上Component注解
public class MyAspect {

    //定义切面，用于后面引用
    @Pointcut("execution(* com.wty.learnsping.controller.*.*(..))")
    public void test(){

    }


    //前置增强
    @Before("test()") //这里面的值是定义的切点的路径，然后去找切点上的exec
    public void before(JoinPoint joinPoint) {
//        System.out.println("Before start");
//        System.out.println(joinPoint.getKind());//method-execution
//        System.out.println(joinPoint.getSignature());// 获取切面增强的方法
//        System.out.println(joinPoint.getSourceLocation());//org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint$SourceLocationImpl@712d35fe
//        System.out.println(joinPoint.getThis());//com.wty.learnsping.controller.RequestController@5483dac2
//        System.out.println(joinPoint.getTarget());//com.wty.learnsping.controller.RequestController@5483dac2
//        System.out.println(joinPoint.getStaticPart());//execution(String com.wty.learnsping.controller.RequestController.test1())
        System.out.println("Before end");
    }


    //后置增强
    @After("execution(* com.wty.learnsping.controller.*.*(..))") //内联pointcut
    public void after(JoinPoint joinPoint){
        System.out.println("After start ");
        System.out.println("After end ");
    }

    //通过returning指定接收返回值的形参名
    @AfterReturning(value = "execution(* com.wty.learnsping.controller.*.*(..))",returning = "returnValue") //内联pointcut
    public void afterReturning(JoinPoint joinPoint,String returnValue){
        System.out.println("AfterReturning start");
        System.out.println("接收到的返回值是"+returnValue);
        System.out.println("AfterReturning end");
    }

    //通过throwing指定抛出的异常的姓名
    @AfterThrowing(value = "execution(* com.wty.learnsping.controller.*.*(..))",throwing = "ex") //内联pointcut
    public void afterThrowing(JoinPoint joinPoint,Exception ex){
        System.out.println("AfterThrowing start");
        System.out.println(ex.getMessage());
        System.out.println("AfterThrowing end");
    }

    @Around("execution(* com.wty.learnsping.controller.*.*(..))") //内联pointcut
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("Around start");
        pjp.proceed();
        System.out.println("Around end");
    }
}
